import Vue from 'vue'
import VueRouter from 'vue-router'



Vue.use(VueRouter)

import login from "../pages/login/login.vue"
import register from "../pages/register/register.vue"
import index from "../pages/index/index.vue"
import list from "../pages/list/list.vue"
import detail from "../pages/detail/detail.vue"
import search from "../pages/search/search.vue"


import home from "../pages/home/home.vue"
import shop from "../pages/shop/shop.vue"
import mine from "../pages/mine/mine.vue"
import fenlei from "../pages/fenlei/fenlei.vue"





const routes = [
  //一级路由
  {
    path: '/login',
    component: login,
    meta: {
      title: "手机登录"
    }
  },
  {
    path: '/register',
    component: register,
    meta: {
      title: "注册"
    }
  },
  {
    path: '/index',
    component: index,
    meta: {
      title: "小u商城"
    },
    //二级路由
    children:[
      {
        path:"home",
        component:home,
        meta:{
          title:"商城"
        }
      },
      {
        path:"shop",
        component:shop,
        meta:{
          title:"购物车"
        }
      },
      {
        path:"mine",
        component:mine,
        meta:{
          title:"会员中心"
        }
      },
      {
        path:"fenlei",
        component:fenlei,
        meta:{
          title:"商品分类"
        }
      },
      {path:"",redirect:"home"}
    ]
  },
  {
    path: "/list",
    component: list,
    meta: {
      title: "商品列表"
    }
  },
  {
    path: "/detail/:id", component: detail,
    meta: {
      title: "商品详情"
    },
  },
  {
    path: "/order", component: ()=>import("../pages/order/order.vue"),
    meta: {
      title: "确认订单"
    },
  },
  {
    path: "/addressList", component: ()=>import("../pages/addressList/addressList.vue"),
    meta: {
      title: "地址列表"
    },
  },
  {
    path: "/addressAdd", component: ()=>import("../pages/addressAdd/addressAdd.vue"),
    meta: {
      title: "地址编辑"
    },
  },
  {
    path: "/pay", component: ()=>import("../pages/pay/pay.vue"),
    meta: {
      title: "支付页面"
    },
  },
  
   {
    path: '*',
    redirect: "/login"
  },
  {
    path: "/search",
    component: search,
    meta: {
      title: "搜索"
    }
  },

]

const router = new VueRouter({
  // history 不带#,hash是带#
  mode:"hash",//hash history  ,默认是hash
  //路由规则
  routes,
  //滚动处理
  scrollBehavior (to, from, savedPosition){
    //如果之前savedPosition没有，滚动到{x:0,y:0}
    //如果之前savedPosition有，滚动到savedPosition
    if(savedPosition){
      return savedPosition
    }else{
      return {x:0,y:0}
    }
  }
})


//全局前置守卫：进入每一个路由都会执行
//to:前往的路由数据 from是从哪来的路由数据  next允许不允许进入
router.beforeEach((to, from,next)=>{
  console.log("全局前置守卫 beforeEach");
  // 1.如果去的登录页，直接进
  if(to.path==="/login"||to.path=="/register"){
    next()
    return;
  }
  // 2.如果去的不是登录，判断islogin有没有。如果有，进
  let islogin=localStorage.getItem("islogin");// "true" null
  if(islogin){
    next()
    return;
  }
  // 3.如果没有，去登录
  next("/login")
})

router.afterEach( route => {
  console.log("全局后置守卫 afterEach");  
})

export default router